package com.igoal.admin.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.igoal.dao.MemberMailDAO;
import com.igoal.dao.RoleDAO;
import com.igoal.dto.MemberMailDTO;
import com.igoal.entity.DataStatus;
import com.igoal.entity.MemberMail;
import com.igoal.entity.Role;
import com.igoal.utils.CalendarUtils;

@Service("memberMailService")
public class MemberMailServiceImpl implements MemberMailService {
	private MemberMailDAO memberMailDAO;	
	private RoleDAO roleDAO;

	@Autowired
	public void setMemberMailDAO(MemberMailDAO memberMailDAO) {
		this.memberMailDAO = memberMailDAO;
	}
	
	@Autowired
	public void setRoleDAO(RoleDAO roleDAO) {
		this.roleDAO = roleDAO;
	}

	public MemberMail save(MemberMail memberMail){
		return memberMailDAO.save(memberMail);
	}
	
	public void save(final List<String> userRoles, final String name, final String subject, final String body, final String scheduledDate, final int priority, final boolean isIgoalDB, final boolean isMarketingDB) {
		MemberMail memberMail = new MemberMail();
		memberMail.setIgoalDB(isIgoalDB);
		memberMail.setMarketingDB(isMarketingDB);
		memberMail.setBody(body);
		memberMail.setSubject(subject);
		memberMail.setName(name);
		memberMail.setPriority(priority);
		
		Date scheduledDateTime = CalendarUtils.parseDate(scheduledDate, "dd/MM/yyyy");
		if(scheduledDateTime == null){
			scheduledDateTime = new Date();
		}
		
		memberMail.setScheduledDate(scheduledDateTime);
		memberMail.setTotalEmailSent(0L);
		memberMail.setStatus(DataStatus.USING);
		memberMail.setCreateDate(new Date());
		memberMail.setUpdateDate(new Date());
		
		final Set<Role> roles = new HashSet<Role>();
		
		if(isIgoalDB){
			String receptionGroups = new String();
			for(String roleName : userRoles){
				Role role = roleDAO.getRoleByName(roleName);
				if(role != null){
					if(StringUtils.isNotBlank(receptionGroups)){
						receptionGroups += ",";
					}
					roles.add(role);
					receptionGroups += role.getName();
				}
			}
			memberMail.setReceptionGroups(receptionGroups);
		}
				
		save(memberMail);
	}

	public MemberMailDTO save(MemberMailDTO memberMailDTO) {
		if(memberMailDTO != null){
			MemberMail entity = null;
			if(memberMailDTO.getId() != null){
				entity = memberMailDAO.get(memberMailDTO.getId());				
			}else{
				entity = new MemberMail();			
				entity.setStatus(DataStatus.USING);
				entity.setCreateDate(new Date());
				entity.setUpdateDate(new Date());
			}
			memberMailDTO.copyAttributesTo(entity);
			entity = memberMailDAO.save(entity);
			memberMailDTO.initFrom(entity);
			
			return memberMailDTO;
		}
		
		return null;
	}

	public List<MemberMailDTO> getAll() {
		List<MemberMail> all = memberMailDAO.getAllDistinct();
		List<MemberMailDTO> result = new ArrayList<MemberMailDTO>();
		for(MemberMail mm : all){
			MemberMailDTO dto = new MemberMailDTO();
			dto.initFrom(mm);
			result.add(dto);
		}
		
		return result;
	}

	public MemberMailDTO get(Long id) {
		if(id != null){
			if(memberMailDAO.exists(id)){
				MemberMail entity = memberMailDAO.get(id);
				MemberMailDTO dto = new MemberMailDTO();
				dto.initFrom(entity);
				return dto;
			}
		}
		
		return null;
	}	
}
